package com.pat.jrgwt.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class FxPositionSize extends Composite
{

    protected NumberFormat m_nf3 = NumberFormat.getFormat("###,##0.000");
    protected FlexTable m_fxPositionTable = new FlexTable(); 
    
    /**
     * Holds the only instance of FxPositionSize
     *
     */
    private static class SingletonHolder
    {
        private static final FxPositionSize instance = new FxPositionSize();
    }
    
    /**
     * Returns the only FxPostionSize instance
     */
    public static FxPositionSize getInstance()
    {
        return SingletonHolder.instance;
    }
    
    private FxPositionSize()
    {
        VerticalPanel panel = new VerticalPanel();
        Label lbl = new Label("FX-Position size 100,000 EUR/EURXXX");
        lbl.setStylePrimaryName("cellBold");
        panel.add(lbl);
        panel.add(m_fxPositionTable);
        
        formatFxPostionTable();
        
        initWidget(panel);
    }
    
    /**
     * Recalculates and re-draws data
     */
    public void recalculate()
    {
        FxPipsView pipsView = FxPipsView.getInstance();
        double v = 0;
        
        for(int month = 1; month < 13; month++)
        {
            v = pipsView.getSumForMonth(month);
//            GWT.log("Got sum for month " + month + ": " + v);
            m_fxPositionTable.setText(month, 2, m_nf3.format(v));
        }
    }

    private void formatFxPostionTable()
    {
        m_fxPositionTable.setBorderWidth(1);
        String[] ar = ValuesStorage.getInstance().getProfitLosses().getExchangeNames();
        for( int i = 0; i < ar.length; i++ )
        {
            m_fxPositionTable.setText(0, i + 1, ar[i]);
            m_fxPositionTable.getCellFormatter().addStyleName(0, i + 1, "cellBold");
            m_fxPositionTable.getCellFormatter().setWidth(0, i + 1, "120px");
        }

        int columns = m_fxPositionTable.getCellCount(0);
        m_fxPositionTable.setText(0, columns, "Sum (Pos. size)");
        m_fxPositionTable.getCellFormatter().setStylePrimaryName(0, columns, "cellBold");
        m_fxPositionTable.getCellFormatter().setWidth(0, columns, "120px");

        for( int row = 1; row < 13; row++ )
        {
            m_fxPositionTable.setText(row, 0, "" + row);
        }
        
        clearValues();
        
        final YearSelector ys = new YearSelector();
        ys.addYearChangeHandler(new ChangeHandler()
        {
            
            @Override
            public void onChange(ChangeEvent event)
            {
                Window.alert("Selected year: " + ys.getSelectedYear());
            }
        });
        m_fxPositionTable.setWidget(0, 0, ys);

    }
    
    /**
     * Zerroes all data cells
     */
    public void clearValues()
    {
        int columns = m_fxPositionTable.getCellCount(0);
        for (int column = 1; column < columns; column++)
        {
            for (int row = 1; row < 13; row++)
            {
                m_fxPositionTable.setText(row, column, "0.0");
            }
        }
    }

}
